package com.game.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.game.common.utils.MailUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.game.common.utils.DataResult;

import com.game.entity.WebConfig;
import com.game.service.WebConfigService;


/**
 * 网站设置
 *
 * @author damow
 * @email *****@mail.com
 * @date 2022-03-26 23:23:14
 */
@Controller
@RequestMapping("/")
public class WebConfigController {
    @Resource
    private WebConfigService webConfigService;


    /**
     * 跳转到页面
     */
    @GetMapping("/index/webConfig")
    public String webConfig(HttpServletRequest request) {
        request.setAttribute("configs", webConfigService.list().stream().collect(
                Collectors.toMap(WebConfig::getName, it -> (StringUtils.isBlank(it.getValue()) ? "" : it.getValue()))));
        return "webConfig/list";
    }


    @ApiOperation(value = "更新")
    @PutMapping("webConfig/update")
    @RequiresPermissions("webConfig:update")
    @ResponseBody
    public DataResult update(@RequestBody String body) {
        try {
            JSONObject json = JSONObject.parseObject(body);
            if (webConfigService.updateByBody(json)) {
                return DataResult.success();
            }
        } catch (Exception e) {
            return DataResult.fail("参数错误");
        }
        return DataResult.fail("参数错误");
    }

    @ApiOperation(value = "清除缓存")
    @PostMapping("/webConfig/clearCache")
    @RequiresPermissions("webConfig:update")
    @ResponseBody
    public DataResult clearCache() {
        webConfigService.init();
        return DataResult.success();
    }

    @ApiOperation(value = "发送测试邮件")
    @PostMapping("webConfig/sendReceiveMailTest")
    @RequiresPermissions("webConfig:update")
    @ResponseBody
    public DataResult sendReceiveMailTest(@RequestBody String body) {
        try {
            JSONObject json = JSONObject.parseObject(body);
            if (null == json) {
                return DataResult.fail("参数错误");
            }
            String mailHost = json.getString("mailHost");
            String mailEncryption = json.getString("mailEncryption");
            String mailPort = json.getString("mailPort");
            String mailUsername = json.getString("mailUsername");
            String mailPassword = json.getString("mailPassword");
            String addr = json.getString("addr");
            if (StringUtils.isBlank(mailHost)) {
                return DataResult.fail("请输入发件服务器");
            }
            if (StringUtils.isBlank(mailEncryption)) {
                return DataResult.fail("请选择加密协议");
            }
            if (StringUtils.isBlank(mailPort)) {
                return DataResult.fail("请输入端口");
            }
            if (StringUtils.isBlank(mailUsername)) {
                return DataResult.fail("请输入邮箱账号");
            }
            if (StringUtils.isBlank(mailPassword)) {
                return DataResult.fail("请输入邮箱密码");
            }
            if (StringUtils.isBlank(addr)) {
                return DataResult.fail("请输入收件邮件地址");
            }
            MailUtil.Mail mail = new MailUtil.Mail();
            mail.setHost(mailHost);
            mail.setPort(mailPort);
            mail.setEncryption(mailEncryption);
            mail.setUsername(mailUsername);
            mail.setPassword(mailPassword);
            mail.setEmailTo(addr);
            mail.setSubject("测试");
            mail.setContent("测试:" + new Date());
            if (!MailUtil.sendMail(mail)) {
                return DataResult.fail("发送失败，请检查配置信息");
            }
            return DataResult.success("发送成功");
        } catch (Exception e) {
            return DataResult.fail("发送失败：" + e.getLocalizedMessage());
        }
    }


    @ApiOperation(value = "查询分页数据")
    @PostMapping("webConfig/listByPage")
    @RequiresPermissions("webConfig:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody WebConfig webConfig) {
        Page page = new Page(webConfig.getPage(), webConfig.getLimit());
        LambdaQueryWrapper<WebConfig> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(Webconfig::getId, webconfig.getId());
        IPage<WebConfig> iPage = webConfigService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
